/**
 * Action 接近于Animaiton
 */

var actionBase = function (){
	this.stepcnt; //全部动作步骤数
	this.animimgs = new Array();
	this.actEx = function(){}; //外接函数
	this.initEx = function(){}; //外接函数
	this.randerlayer =LAYER_BG; // =LAYER_AUTO;
	this.x;
	this.y;
	this.autoreplay =false;
	this.step =0;
	this.Statehold = false; //动作保持。 执行后一直保持最后的一帧
	this.Eatmsg =false;//动作进行中时，系统不接受其他消息
	this.State = 0;
	this.intval =1;
	this.GoNextSnario =false; //完成后是否会触动剧情下一步
	this.DisableME = false;//完成后是否屏蔽地图事件
	
	//this.getranderLayer;
	
	this.sst =0;
}

actionBase.prototype.init = function (){ 
	this.initEx();
}
actionBase.prototype.act = function (){ 
	
	this.actEx();
	
	this.State =1; //1 动作中 ； 2 完成
	
	if (this.autoreplay && this.step == this.stepcnt) {this.step=0}
	
	if (this.Statehold && this.step == this.stepcnt) {
		this.step=this.stepcnt-1;
	}
	
	if (this.step<this.stepcnt){
		
		if (this.Statehold && this.step == this.stepcnt-1) {
			this.State =2;
			if (this.GoNextSnario) {GoNextSnario();this.GoNextSnario=false;}
			if (this.Eatmsg) bEatmsg=false;
		}
		
		if (this.Eatmsg && this.State!=2 ) bEatmsg=true;
		
		if (this.animimgs[this.step]!=null){
			//渲染图层
			switch (this.randerlayer){
			case LAYER_BY:
				RanderJobLst_Beyound.push(new RanderJob());
				RanderJobLst_Beyound[RanderJobLst_Beyound.length-1].x = this.x;
				RanderJobLst_Beyound[RanderJobLst_Beyound.length-1].y = this.y;
				RanderJobLst_Beyound[RanderJobLst_Beyound.length-1].imgobj = this.animimgs[this.step];
				break;
			case LAYER_BG:
				RanderJobLst_Backgournd.push(new RanderJob());
				RanderJobLst_Backgournd[RanderJobLst_Backgournd.length-1].x = this.x;
				RanderJobLst_Backgournd[RanderJobLst_Backgournd.length-1].y = this.y;
				RanderJobLst_Backgournd[RanderJobLst_Backgournd.length-1].imgobj = this.animimgs[this.step];
				break;
			case LAYER_BG_0:
				RanderJobLst_Backgournd0.push(new RanderJob());
				RanderJobLst_Backgournd0[RanderJobLst_Backgournd0.length-1].x = this.x;
				RanderJobLst_Backgournd0[RanderJobLst_Backgournd0.length-1].y = this.y;
				RanderJobLst_Backgournd0[RanderJobLst_Backgournd0.length-1].imgobj = this.animimgs[this.step];
				break;
			}
		}
		this.sst++;
		if (this.sst>=this.intval){
			this.step++;
			this.sst =0;
		}
		
	}else{
		if (this.GoNextSnario) {GoNextSnario();this.GoNextSnario=false;}
		if (this.Eatmsg) bEatmsg=false;
		this.State = 2;
		
	}
	
}


//这个类包含了4个方向的走动，但是每个方向需要一个实例
var walkaction = function (){
	this.sprite;
	this.direction = 0;
	
	this.Statehold = true;
	this.stepcnt = WALKSTEPCNT;
	
	this.initEx= function(){
		this.x = this.sprite.Stepx *WALKSTEPD*WALKSTEPCNT;
		this.y = (this.sprite.Stepy+1) *WALKSTEPD*WALKSTEPCNT-SPRITH;
		
		var checkx; var checky;
		checky = this.sprite.Stepy;
		checkx = this.sprite.Stepx;
		switch (this.direction){
		case CONST_LEFT:
			checkx--;break;
		case CONST_RIGHT:
			checkx++;break;
		case CONST_UP:
			checky--;break;
		case CONST_DOWN:
			checky++;break;
		//8方向	
		case CONST_LEFT_UP:
			checkx--;checky--;break;
		case CONST_RIGHT_UP:
			checkx++;checky--;break;
		case CONST_LEFT_DOWN:
			checkx--;checky++;break;
		case CONST_RIGHT_DOWN:
			checkx++;checky++;break;
		}
		mf = mapdata[checky][checkx];
		
		
		this.sprite.abx = this.x;
		this.sprite.aby = this.y;
		
		if (mf !='0'){
			this.sprite.Stepx =checkx;
			this.sprite.Stepy =checky;
			
			//TODO 不能应用intval
			//可以走动，位移
			this.actEx = function (){
				if (this.Statehold && this.step == this.stepcnt) return;
				switch (this.direction){
				case CONST_LEFT:
					this.x -=WALKSTEPD;break;
				case CONST_RIGHT:
					this.x +=WALKSTEPD;break;
				case CONST_UP:
					this.y -=WALKSTEPD;break;
				case CONST_DOWN:
					this.y +=WALKSTEPD;break;	
				//8方向	
				case CONST_LEFT_UP:
					this.y -=WALKSTEPD45;this.x -=WALKSTEPD45;break;
				case CONST_RIGHT_UP:
					this.y -=WALKSTEPD45;this.x +=WALKSTEPD45;break;
				case CONST_LEFT_DOWN:
					this.y +=WALKSTEPD45;this.x -=WALKSTEPD45;break;
				case CONST_RIGHT_DOWN:
					this.y +=WALKSTEPD45;this.x +=WALKSTEPD45;break;	
					
				}
				this.sprite.abx = this.x;
				this.sprite.aby = this.y;
			}
			
			
		
		} else {
			
			this.step = this.stepcnt-1;
			
		}
		if (mapdata[this.sprite.Stepy][this.sprite.Stepx]
		== "3")
			this.randerlayer = LAYER_BY;
		else
			this.randerlayer = LAYER_BG;
	}
	
}
walkaction.prototype =new actionBase();

/*这里是类继承的好例子 ,但是因为有了actEx ，可以不用
walkaction.prototype.act()= function (){
	actionBase.prototype.act.call(this);
}*/


//用于定位
var locaction = function (){
	this.sprite;
	this.Statehold = true;
	this.stepcnt = 1;
	this.GoNextSnario =true;
	
	this.init= function(){
		this.x = this.sprite.Stepx*WALKSTEPD*WALKSTEPCNT;
		this.y = (this.sprite.Stepy+1)*WALKSTEPD*WALKSTEPCNT-SPRITH;
		
		this.sprite.abx = this.x;
		this.sprite.aby = this.y;
		
		if (mapdata[this.sprite.Stepy][this.sprite.Stepx]
		== "3")
			this.randerlayer = LAYER_BY;
		else
			this.randerlayer = LAYER_BG;
		
		//GoNextSnario();
	}
	
}
locaction.prototype =new actionBase();

//卷起打开屏幕
var wrapmap = function (){
	
	this.Eatmsg =true;
	this.stepcnt = 20;
	
	
	this.actEx = function (){
		
		if (this.step>=this.stepcnt) return;
		
		if (this.step<10) 
			stepw = 60;
		else 
			stepw = -60;
		
		var x=$("#qz1").offset().left + stepw;  
		var y=$("#qz1").offset().top;
		$("#qz1").offset({top:y,left:x});
		x=$("#qz2").offset().left + stepw;  
		y=$("#qz2").offset().top;
		$("#qz2").offset({top:y,left:x});
		x=$("#qz3").offset().left + stepw;  
		y=$("#qz3").offset().top;
		$("#qz3").offset({top:y,left:x});
		
		x=$("#jz4").offset().left + stepw;  
		y=$("#jz4").offset().top;
		$("#jz4").offset({top:y,left:x});
		$("#jz4").width($("#jz4").width()-stepw);
		
		x=$("#jz5").offset().left + stepw;  
		y=$("#jz5").offset().top;
		$("#jz5").offset({top:y,left:x});
		$("#jz5").width($("#jz5").width()-stepw);
		
		cxt.clearRect(this.step*stepw, 0, stepw,SCREENH);
		
	}
	
}
wrapmap.prototype =new actionBase();

var loadingSrc = function (){
	
	this.Eatmsg =true;
	this.stepcnt = 100;
	this.GoNextSnario = true;
	this.img = new Image(); 
	this.img.src ="/RPG/sucai/ui/loading.png";
	this.img2 = new Image(); 
	this.img2.src ="/RPG/sucai/ui/loadingBak.jpg";
	
	this.actEx = function (){
		if (this.step>=this.stepcnt) return;
		
		cxt.clearRect(0, 0, SCREENW,SCREENH);
		
		cxt.globalAlpha = loadedcnt/PreLoadlst.length;
		cxt.drawImage(this.img2,0,0);
		cxt.globalAlpha =1;
		cxt.drawImage(this.img,20,20);
		
		cxt.font        = "normal 12px Arial";
		cxt.strokeStyle = "#000000";
		
		var mtext =""; 
		for (var i=0;i<this.step % 3;i++) mtext += '.';
		
		cxt.fillText("请稍等" + mtext, 20, 100);
		cxt.fillText( Math.round(loadedcnt/PreLoadlst.length*100) + "%", 80, 100);  
		
		if (this.step>19) this.step=0;

		if (loadedcnt>=PreLoadlst.length) {
			this.step=this.stepcnt;
			cxt.globalAlpha =1;
			isPreloading =0;
			PreLoadlst =[];
			//GoNextSnario();
		}
	}
}
loadingSrc.prototype =new actionBase();

//渐变出现，现在只能试用给鲤鱼精
var Appear = function (){
	this.stepcnt=10;
	this.randerlayer =LAYER_BY;
	this.Statehold = true; 
	this.GoNextSnario =true; 
	this.width;
	this.height;
	this.m_cxt;
	this.imgBackup;
	this.m_canvas;
	this.initEx= function(){
		//Todo 现在只有一个图片，以后可以是多个的动画
		this.m_canvas = document.createElement('canvas');
		this.m_canvas.width = this.width;
		this.m_canvas.height = this.height;
		this.m_cxt=this.m_canvas.getContext('2d');
		this.imgBackup = this.animimgs[0];
		
		for (var i=0;i<this.stepcnt;i++){
			this.animimgs[i]=this.m_canvas;
		}
	}
	this.actEx = function (){
		this.m_cxt.clearRect(0, 0, this.width,this.height);
		this.m_cxt.globalAlpha = 0.1*this.step;
		this.m_cxt.drawImage(this.imgBackup,0,0);
		if (this.m_cxt.globalAlpha <1) this.y-=4;
		
	}
	
}
Appear.prototype =new actionBase();

//渐变消失，被消灭时使用
var Disappear = function (){
	this.stepcnt=10;
	this.randerlayer =LAYER_BY;
	this.Statehold = true; 
	this.GoNextSnario =true; 
	this.width;
	this.height;
	this.m_cxt;
	this.imgBackup;
	this.m_canvas;
	this.initEx= function(){
		//Todo 现在只有一个图片，以后可以是多个的动画
		this.m_canvas = document.createElement('canvas');
		this.m_canvas.width = this.width;
		this.m_canvas.height = this.height;
		this.m_cxt=this.m_canvas.getContext('2d');
		this.imgBackup = this.animimgs[0];
		for (var i=0;i<this.stepcnt;i++){
			this.animimgs[i]=this.m_canvas;
		}
		
	}
	this.actEx = function (){
		this.m_cxt.clearRect(0, 0, this.width,this.height);
		if (this.step % 2 == 0){
		this.m_cxt.globalAlpha = 1-(0.1*this.step);
		this.m_cxt.drawImage(this.imgBackup,0,0);
		}
		
		
	}
	
}
Disappear.prototype =new actionBase();
